package com.csd.dao;
// default package

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.csd.pojo.Score;

/**
 	* A data access object (DAO) providing persistence and search support for Score entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see .Score
  * @author MyEclipse Persistence Tools 
 */

public class ScoreDAO extends HibernateDaoSupport  {
    private static final Log log = LogFactory.getLog(ScoreDAO.class);
	//property constants
	public static final String PROPHASESCORE = "prophasescore";
	public static final String METAPHASESCORE = "metaphasescore";
	public static final String TELOPHASESCORE = "telophasescore";
	public static final String ARSCORE = "arscore";
	public static final String ERSCORE = "erscore";



	protected void initDao() {
		//do nothing
	}
    
    public void save(Score transientInstance) {
        log.debug("saving Score instance");
        try {
            getHibernateTemplate().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(Score persistentInstance) {
        log.debug("deleting Score instance");
        try {
            getHibernateTemplate().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public Score findById( java.lang.Integer id) {
        log.debug("getting Score instance with id: " + id);
        try {
            Score instance = (Score) getHibernateTemplate()
                    .get(ScoreDAO.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    /**
     * 根据项目id查找成绩
     * @param id 项目id
     * @return
     */
    public Score findByProjectID(Integer id) {
        log.debug("getting Score instance with projectID: " + id);
        Score score = null;
        try {
        	String hql = "from Score as score where score.project.id=?";
           List list = getHibernateTemplate().find(hql,id);
           if(list!= null && list.size() > 0)
           {
        	   score = (Score)list.get(0);
           }
        } catch (RuntimeException re) {
            log.error("find by projectID fail", re);
            throw re;
        }
        return score;
    }
    
    /**
     * 更新成绩
     * @param score
     */
    public void update(Score score)
    {
    	log.debug("更新成绩开始");
    	try {
    		getHibernateTemplate().update(score);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			log.error("更新成绩错误", e);
		}
    	
    }
    
    public List findByExample(Score instance) {
        log.debug("finding Score instance by example");
        try {
            List results = getHibernateTemplate().findByExample(instance);
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Score instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Score as model where model." 
         						+ propertyName + "= ?";
		 return getHibernateTemplate().find(queryString, value);
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByProphasescore(Object prophasescore
	) {
		return findByProperty(PROPHASESCORE, prophasescore
		);
	}
	
	public List findByMetaphasescore(Object metaphasescore
	) {
		return findByProperty(METAPHASESCORE, metaphasescore
		);
	}
	
	public List findByTelophasescore(Object telophasescore
	) {
		return findByProperty(TELOPHASESCORE, telophasescore
		);
	}
	
	public List findByArscore(Object arscore
	) {
		return findByProperty(ARSCORE, arscore
		);
	}
	
	public List findByErscore(Object erscore
	) {
		return findByProperty(ERSCORE, erscore
		);
	}
	

	public List findAll() {
		log.debug("finding all Score instances");
		try {
			String queryString = "from Score";
		 	return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public Score merge(Score detachedInstance) {
        log.debug("merging Score instance");
        try {
            Score result = (Score) getHibernateTemplate()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Score instance) {
        log.debug("attaching dirty Score instance");
        try {
            getHibernateTemplate().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Score instance) {
        log.debug("attaching clean Score instance");
        try {
            getHibernateTemplate().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }

	public static ScoreDAO getFromApplicationContext(ApplicationContext ctx) {
    	return (ScoreDAO) ctx.getBean("ScoreDAO");
	}
}